<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>上传图片</title>
  </head>
  <body>
    <form id="myForm" name="myForm" enctype="multipart/form-data">
      <h1>头像</h1>
      <img id="touxiang" style="width: 150px" src="" alt="" />
      <div>
        <label for="userfile">上传头像:</label>
        <input type="file" id="userfile" name="userfile" />
      </div>
      <input type="button" value="上传" id="submit" />
    </form>
  </body>
  <script src="./js/jquery.min.js"></script>
  <script>
    let id = "5f6c0ee441511d26c8a4e18b";
    //获取头像显示
    function getpic() {
      $.ajax({
        url: "/user/getuser/" + id,
        type: "get",
        dataType: "json",
        success: function (res) {
          console.log(res, 999);
          $("#touxiang").attr("src", res.data[0].pic);
        },
      });
    }

    getpic();

    $("#submit").click(() => {
      // console.log($("#userfile")[0].files[0]);
      let formData = new FormData(); //实例化
      formData.append("avatar", $("#userfile")[0].files[0]);
      formData.append("id", id);
      $.ajax({
        url: "/user/headpic",
        type: "post",
        cache: false,
        data: formData,
        processData: false,
        dataType: "json",
        contentType: false, //multipart/form-data
        success: function (res) {
          console.log(res);
          getpic();
        },
      });
    });

    //浅拷贝
    // let arr = [1, 2, 3]; //引用类型
    // let arr2 = arr; //引用：把地址给人家
    // arr2.push(666);
    // console.log(arr, arr2); //修改某一个会影响另一个

    // //深拷贝-深克隆
    // let arr3 = [1, 2, 3];

    // function clone(obj) {
    //   let str = JSON.stringify(obj);
    //   let newobj = JSON.parse(str);
    //   return newobj;
    // }

    // let arr4 = clone(arr3);
    // arr4.push("666");
    // console.log(arr3, arr4, 999);

    // let arr5 = [4, 5, [6, 8]];
    // function clone2(obj) {
    //   let newarr = [];
    //   obj.forEach((item) => {
    //     newarr.push(item);
    //   });
    //   return newarr;
    // }

    // let arr6 = clone2(arr5);
    // arr6.push(1234);
    // console.log(arr5, arr6, 123);

    // let a = 1; //值类型
    // let b = a;
    // b = 888;
    // console.log(a, b);
  </script>
</html>
